package com.njust.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.njust.common.Const;
import com.njust.dataobject.GearInfo;
import com.njust.dataobject.MotorInfo;
import com.njust.dataobject.TrainInfo;
import com.njust.dataobject.TrainParam;
import com.njust.form.SlotForm;
import com.njust.mapper.GearInfoMapper;

import com.njust.mapper.MotorInfoMapper;
import com.njust.mapper.TrainInfoMapper;
import com.njust.mapper.TrainParamMapper;
import com.njust.service.GearService;
import com.njust.utils.BigDecimalUtil;
import com.njust.utils.CompareUtil;
import com.njust.utils.DateUtil;
import com.njust.utils.PageUtils;
import com.njust.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.*;

/**
 * Created by zhujiawei on 2017/12/30.
 */
@Service
@Slf4j
public class GearServiceImpl implements GearService{

    @Autowired
    private GearInfoMapper gearInfoMapper;

    @Autowired
    private MotorInfoMapper motorInfoMapper;

    @Autowired
    private TrainInfoMapper trainInfoMapper;

    @Autowired
    private TrainParamMapper trainParamMapper;

    /** CorsFilter
     * 根据列车记录编号 和 电机号 查询到所有齿的数据
     * 已修改
     * @param trainOnlyid
     * @param motorNum
     * @return
     */
  /*  @Override
    public ResultVO findByTrainOnlyidAndMotorId(Long trainOnlyid,Integer motorNum,Integer trainDirection){
        List<GearVO> gearVOList=new ArrayList<>();
        MotorInfo motorInfo = motorInfoMapper.findByTrainIdAndMotorNum(trainOnlyid, motorNum);
        TrainParam trainParam = trainParamMapper.findByUdFlag(trainDirection);
        if(motorInfo==null){
            return ResultVO.createByErrorMessage("无数据");
        }
        List<GearInfo> gearInfoList = gearInfoMapper.findByMotorId(motorInfo.getMotorId());
        if(gearInfoList==null){
            return ResultVO.createByErrorMessage("无数据");
        }
        for (GearInfo gearInfo:gearInfoList){
            GearVO gearVO=new GearVO();
            BeanUtils.copyProperties(gearInfo,gearVO);
            double LslotDepth = BigDecimalUtil.dUtil(gearInfo.getLslotDepth(), 2);
            double RslotDepth = BigDecimalUtil.dUtil(gearInfo.getRslotDepth(), 2);
            gearVO.setLslotDepth(LslotDepth);
            gearVO.setRslotDepth(RslotDepth);


            if(gearInfo.getLgapValue()!=null || trainParam!=null){
                Integer result = CompareUtil.compare(Const.GEAR, trainParam.getGapWarn()
                        , trainParam.getGapAlarm(), gearInfo.getLgapValue());
                gearVO.setLgapStatus(result);
            }
            if(gearInfo.getRgapValue()!=null || trainParam!=null){
                Integer result = CompareUtil.compare(Const.GEAR, trainParam.getGapWarn()
                        , trainParam.getGapAlarm(), gearInfo.getRgapValue());
                gearVO.setRgapStatus(result);
            }
            //槽楔 需要判断

            if(gearInfo.getLslotValue()!=null || trainParam!=null){
                Integer result = CompareUtil.compare(Const.SLOT, trainParam.getSlotWarn()
                        , trainParam.getSlotAlarm(), gearInfo.getLslotValue());
                gearVO.setLslotStatus(result);
            }

            if(gearInfo.getRslotValue()!=null || trainParam!=null){
                Integer result = CompareUtil.compare(Const.SLOT, trainParam.getSlotWarn()
                        , trainParam.getSlotAlarm(), gearInfo.getRslotValue());
                gearVO.setRslotStatus(result);
            }
            //槽深

            if(gearInfo.getLslotDepth()!=null || trainParam!=null){
                *//**
                 * 这里需要进行重新判断,需要添加槽深的上限预警报警信息
                 *//*
                Integer result = CompareUtil.compare(Const.SLOTDEPTH, trainParam.getSlotdWarn()
                        , trainParam.getSlotdAlarm(), gearInfo.getLslotDepth());
                if(result==0){
                    result =CompareUtil.compare(Const.SLOTUDEPTH,trainParam.getSlotuWarm(),trainParam.getSlotuAlarm(),gearInfo.getLslotDepth());
                }
                gearVO.setLslotdStatus(result);
            }

            if(gearInfo.getRslotDepth()!=null || trainParam!=null){
                Integer result = CompareUtil.compare(Const.SLOTDEPTH, trainParam.getSlotdWarn()
                        , trainParam.getSlotdAlarm(), gearInfo.getRslotDepth());
                if(result==0){
                    result = CompareUtil.compare(Const.SLOTUDEPTH,trainParam.getSlotuWarm(),trainParam.getSlotuAlarm(),gearInfo.getRslotDepth());
                }
                gearVO.setRslotdStatus(result);
            }

            //槽深
            gearVOList.add(gearVO);
        }
        return ResultVO.createBySuccess(gearVOList);
    }
*/
    @Override
    public Map<String,Double> findByTrainOnlyidMaxDifference(Long trainOnlyid,Integer motorNum,Integer trainDirection){
        List<GearVO> gearVOList=new ArrayList<>();
        TrainInfo trainInfo = trainInfoMapper.selectTrainInfoById(trainOnlyid);
        Long trainOnlyid2 =0l;
        List<TrainInfo> trainInfoByTrainIdAndDirection = trainInfoMapper.findTrainInfoByTrainIdAndDirection(trainInfo.getTrainId(), trainInfo.getTrainDirection());
        for (int i = 0; i <trainInfoByTrainIdAndDirection.size() ; i++) {
            if (trainInfoByTrainIdAndDirection.get(i).getTrainOnlyid().longValue()==trainOnlyid.longValue()){
                trainOnlyid2=trainInfoByTrainIdAndDirection.get(i-1).getTrainOnlyid();
            }
        }
        MotorInfo motorInfo = motorInfoMapper.findByTrainIdAndMotorNum(trainOnlyid, motorNum);
        List<GearInfo> gearInfoList2 =new ArrayList<>();
        if(trainOnlyid2!=null) {
            MotorInfo motorInfo2 = motorInfoMapper.findByTrainIdAndMotorNum(trainOnlyid2, motorNum);
            if (motorInfo2!=null) {
                gearInfoList2 = gearInfoMapper.findByMotorId(motorInfo2.getMotorId());
            }
        }
        TrainParam trainParam = trainParamMapper.findByUdFlag(trainDirection);
        if(motorInfo==null){
            return null;
        }
        List<GearInfo> gearInfoList = gearInfoMapper.findByMotorId(motorInfo.getMotorId());
        if(gearInfoList==null){
            return null;
        }
        for (int i=0;i<gearInfoList.size();i++){
            GearVO gearVO=new GearVO();
            BeanUtils.copyProperties(gearInfoList.get(i),gearVO);
            double LslotDepth = BigDecimalUtil.dUtil(gearInfoList.get(i).getLslotDepth(), 2);
            double RslotDepth = BigDecimalUtil.dUtil(gearInfoList.get(i).getRslotDepth(), 2);
            gearVO.setLslotDepth(LslotDepth);
            gearVO.setRslotDepth(RslotDepth);
            if (gearInfoList2.size()>79&&(gearInfoList2.get(i).getGearNum()>3&&(gearInfoList2.get(i).getGearNum()<16)||(gearInfoList2.get(i).getGearNum()>64&&gearInfoList2.get(i).getGearNum()<78))) {
                gearVO.setRgapDifference(Math.abs(gearInfoList2.get(i).getRgapValue()-gearInfoList.get(i).getRgapValue()));
                gearVO.setLgapDifference(Math.abs(gearInfoList2.get(i).getLgapValue()-gearInfoList.get(i).getLgapValue()));
            }else {
                gearVO.setRgapDifference(0.0);
                gearVO.setLgapDifference(0.0);
            }
            //槽深
            gearVOList.add(gearVO);
        }
        Double leftMax =0.0;
        Double rightMax=0.0;
        for( GearVO gearVO: gearVOList){
            if (gearVO.getRgapDifference()!=null&&gearVO.getRgapDifference()>rightMax){
                rightMax=gearVO.getRgapDifference();
                System.out.println(rightMax);
            }
            if (gearVO.getRgapDifference()!=null&&gearVO.getLgapDifference()>leftMax){
                leftMax=gearVO.getLgapDifference();
            }
        }
        Map<String,Double> map= new HashMap<>(2);
        map.put("leftMax",leftMax);
        map.put("rightMax",rightMax);
        return map;
    }

    @Override
    public ResultVO findByTrainOnlyidAndMotorId(Long trainOnlyid,Integer motorNum,Integer trainDirection){
        List<GearVO> gearVOList=new ArrayList<>();
        TrainInfo trainInfo = trainInfoMapper.selectTrainInfoById(trainOnlyid);
        Long trainOnlyid2 =0l;
        List<TrainInfo> trainInfoByTrainIdAndDirection = trainInfoMapper.findTrainInfoByTrainIdAndDirection(trainInfo.getTrainId(), trainInfo.getTrainDirection());
        for (int i = 0; i <trainInfoByTrainIdAndDirection.size() ; i++) {
            if (trainInfoByTrainIdAndDirection.get(i).getTrainOnlyid().longValue()==trainOnlyid.longValue()){
                trainOnlyid2=trainInfoByTrainIdAndDirection.get(i-1).getTrainOnlyid();
            }
        }
        MotorInfo motorInfo = motorInfoMapper.findByTrainIdAndMotorNum(trainOnlyid, motorNum);
        List<GearInfo> gearInfoList2 =new ArrayList<>();
        if(trainOnlyid2!=null) {
            MotorInfo motorInfo2 = motorInfoMapper.findByTrainIdAndMotorNum(trainOnlyid2, motorNum);
            if (motorInfo2!=null) {
                gearInfoList2 = gearInfoMapper.findByMotorId(motorInfo2.getMotorId());
            }
        }
        TrainParam trainParam = trainParamMapper.findByUdFlag(trainDirection);
        if(motorInfo==null){
            return ResultVO.createByErrorMessage("无数据");
        }
        List<GearInfo> gearInfoList = gearInfoMapper.findByMotorId(motorInfo.getMotorId());
        if(gearInfoList==null){
            return ResultVO.createByErrorMessage("无数据");
        }
        for (int i=0;i<gearInfoList.size();i++){
            GearVO gearVO=new GearVO();
            BeanUtils.copyProperties(gearInfoList.get(i),gearVO);
            double LslotDepth = BigDecimalUtil.dUtil(gearInfoList.get(i).getLslotDepth(), 2);
            double RslotDepth = BigDecimalUtil.dUtil(gearInfoList.get(i).getRslotDepth(), 2);
            gearVO.setLslotDepth(LslotDepth);
            gearVO.setRslotDepth(RslotDepth);


            if(gearInfoList.get(i).getLgapValue()!=null || trainParam!=null){
                Integer result = CompareUtil.compare(Const.GEAR, trainParam.getGapWarn()
                        , trainParam.getGapAlarm(), gearInfoList.get(i).getLgapValue());
                gearVO.setLgapStatus(result);
            }
            if(gearInfoList.get(i).getRgapValue()!=null || trainParam!=null){
                Integer result = CompareUtil.compare(Const.GEAR, trainParam.getGapWarn()
                        , trainParam.getGapAlarm(), gearInfoList.get(i).getRgapValue());
                gearVO.setRgapStatus(result);
            }
            //槽楔 需要判断

            if(gearInfoList.get(i).getLslotValue()!=null || trainParam!=null){
                Integer result = CompareUtil.compare(Const.SLOT, trainParam.getSlotWarn()
                        , trainParam.getSlotAlarm(), gearInfoList.get(i).getLslotValue());
                gearVO.setLslotStatus(result);
            }

            if(gearInfoList.get(i).getRslotValue()!=null || trainParam!=null){
                Integer result = CompareUtil.compare(Const.SLOT, trainParam.getSlotWarn()
                        , trainParam.getSlotAlarm(), gearInfoList.get(i).getRslotValue());
                gearVO.setRslotStatus(result);
            }
            //槽深

            if(gearInfoList.get(i).getLslotDepth()!=null || trainParam!=null){
                /**
                 * 这里需要进行重新判断,需要添加槽深的上限预警报警信息
                 */
                Integer result = CompareUtil.compare(Const.SLOTDEPTH, trainParam.getSlotdWarn()
                        , trainParam.getSlotdAlarm(), gearInfoList.get(i).getLslotDepth());
                if(result==0){
                    result =CompareUtil.compare(Const.SLOTUDEPTH,trainParam.getSlotuWarm(),trainParam.getSlotuAlarm(),gearInfoList.get(i).getLslotDepth());
                }
                gearVO.setLslotdStatus(result);
            }

            if(gearInfoList.get(i).getRslotDepth()!=null || trainParam!=null){
                Integer result = CompareUtil.compare(Const.SLOTDEPTH, trainParam.getSlotdWarn()
                        , trainParam.getSlotdAlarm(), gearInfoList.get(i).getRslotDepth());
                if(result==0){
                    result = CompareUtil.compare(Const.SLOTUDEPTH,trainParam.getSlotuWarm(),trainParam.getSlotuAlarm(),gearInfoList.get(i).getRslotDepth());
                }
                gearVO.setRslotdStatus(result);
            }
            if (gearInfoList2.size()>79&&(gearInfoList2.get(i).getGearNum()>3&&(gearInfoList2.get(i).getGearNum()<16)||(gearInfoList2.get(i).getGearNum()>64&&gearInfoList2.get(i).getGearNum()<78))) {
                gearVO.setRgapDifference(Math.abs(gearInfoList2.get(i).getRgapValue()-gearInfoList.get(i).getRgapValue()));
                gearVO.setLgapDifference(Math.abs(gearInfoList2.get(i).getLgapValue()-gearInfoList.get(i).getLgapValue()));
            }else {
                gearVO.setRgapDifference(0.0);
                gearVO.setLgapDifference(0.0);
            }
            //槽深
            gearVOList.add(gearVO);
        }
        return ResultVO.createBySuccess(gearVOList);
    }
    /**
     * 已修改
     * 槽楔趋势查询分析
     * @param slotForm
     * @return
     */
    @SuppressWarnings("unchecked")
    public ResultVO slotTrendencyQuery(SlotForm slotForm) throws ParseException {
        List<SlotTendencyVO> slotTendencyVOList=new ArrayList<>();
        //趋势图
        if(slotForm.getPage()==null && slotForm.getPageSize()==null){

            //根据时间段选出列车记录编号
            Date start= DateUtil.String2Date(slotForm.getStartTime());
            Date end=DateUtil.String2Date(slotForm.getEndTime());
            List<TrainInfo> trainInfoList = trainInfoMapper
                    .findByTrainDateAndDirection(start, end, slotForm.getTrainId(),slotForm.getTrainDirection());
            if(trainInfoList.size()==0){
                return ResultVO.createByErrorMessage("无数据");
            }
            //数据封装
            assembleSlotInfo(slotForm,slotTendencyVOList,trainInfoList);
            return ResultVO.createBySuccess(slotTendencyVOList);
        }else {
            Date start= DateUtil.String2Date(slotForm.getStartTime());
            Date end=DateUtil.String2Date(slotForm.getEndTime());
            PageHelper.startPage(slotForm.getPage(),slotForm.getPageSize());
            List<TrainInfo> trainInfoList = trainInfoMapper
                    .findByTrainDateAndDirection(start, end, slotForm.getTrainId(),slotForm.getTrainDirection());
            PageInfo<TrainInfo> pageInfo=new PageInfo<>(trainInfoList);
            if(trainInfoList.size()==0){
                return ResultVO.createByErrorMessage("无数据");
            }
            assembleSlotInfo(slotForm,slotTendencyVOList,trainInfoList);
            PageResultVo pageResultVo = new PageResultVo();
            PageUtils.pageUtil(pageInfo,pageResultVo);
            pageResultVo.setList(slotTendencyVOList);
            return ResultVO.createBySuccess(pageResultVo);
        }
    }

    private void assembleSlotInfo(SlotForm slotForm, List<SlotTendencyVO> slotTendencyVOList, List<TrainInfo> trainInfoList) {
        for (TrainInfo trainInfo:trainInfoList){
            SlotTendencyVO slotTendencyVO=new SlotTendencyVO();
            MotorInfo motorInfo = motorInfoMapper
                    .findByTrainIdAndMotorNum(trainInfo.getTrainOnlyid(), slotForm.getMotorNum());
            if(motorInfo==null){
                continue;
            }
            slotTendencyVO.setTrainDate(DateUtil.Date2String(trainInfo.getTrainDate()));
            slotTendencyVO.setTrainId(trainInfo.getTrainId());
            slotTendencyVO.setTrainDirection(trainInfo.getTrainDirection());
            slotTendencyVO.setMotorNum(motorInfo.getMotorNum());
            //电机编号和具体的齿编号 查询到结果7 73齿的槽楔数据
            List<GearInfo> gearInfoList = gearInfoMapper.
                    findByMotorIdAndInGearNum(motorInfo.getMotorId(), slotForm.getFirstSlot(), slotForm.getSecondSlot());
            if(gearInfoList.size()==0){
                continue;
            }
            List<SlotVO> slotVOList = assembleGearInfo(gearInfoList);
            slotTendencyVO.setSlotVOList(slotVOList);
            slotTendencyVOList.add(slotTendencyVO);
        }
    }

    /**
     * 转化为 分装槽楔的数据
     * @param gearInfoList
     * @return
     */
    public List<SlotVO> assembleGearInfo(List<GearInfo> gearInfoList){
        List<SlotVO> slotVOList=new ArrayList<>();
        for (GearInfo gearInfo:gearInfoList){
            SlotVO slotVO=new SlotVO();
            slotVO.setGearNum(gearInfo.getGearNum());
            slotVO.setLslotValue(gearInfo.getLslotValue());
            slotVO.setRslotValue(gearInfo.getRslotValue());
            slotVO.setLslotDepth(gearInfo.getLslotDepth());
            slotVO.setRslotDepth(gearInfo.getRslotDepth());
            slotVOList.add(slotVO);
        }
        return slotVOList;
    }
}
